package com.rr.sort;

import java.util.Arrays;

/**
 * @author RR
 * @date 2022/12/28 12:34
 */
public class InsertSort {
    public static void insertSort(int[] array) {
        int length;
        if (array == null || (length = array.length) <= 1) {
            return;
        }
        for (int i = 1; i < length; i++) {
            int waitForInsertNum = array[i];
            int j;
            for (j = i - 1; j >= 0; j--) {
                if (array[j] > waitForInsertNum) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j+1] = waitForInsertNum;
        }
    }

    public static void main(String[] args) {
        int[] array = new int[]{5, 4, 3, 2, 1};
        insertSort(array);
        System.out.println(Arrays.toString(array));
    }
}
